package cn.dgut.training.controller;

import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.transaction.OrdersSubDTO;
import cn.dgut.training.entity.dto.OrdersDTO;
import cn.dgut.training.entity.user.User;
import cn.dgut.training.entity.vo.OrderGoodsVO;
import cn.dgut.training.entity.vo.OrdersVO;
import cn.dgut.training.service.IOrderService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private UserService userService;

    @Resource
    private IOrderService orderService;

    /**
     * 根据条件分页查询订单列表
     * @param ordersDTO
     * @return
     */
    @PostMapping("/list")
    public CommonResponse selectLimit(@LoginUser Long userId,@RequestBody @Valid OrdersDTO ordersDTO){
        ordersDTO.setUserId(userId);
        IPage<OrdersVO> ordersVOIPage = orderService.listOrderLimit(ordersDTO);
        BaseVO<OrdersVO> ordersVOBaseVO=new BaseVO<>(ordersVOIPage);
        return CommonResponse.success(ordersVOBaseVO);
    }

    /**
     * 通过订单的id查询订单详情
     * @param id
     * @return
     */
//    @PostMapping("/detail")
    @GetMapping("/detail")
    public CommonResponse selectById(@LoginUser Long userId,@RequestParam("id") Long id){
        OderDetails oderDetails=new OderDetails();
        OrdersVO ordersVO=orderService.selectOrderById(id);
        oderDetails.orderGoods=ordersVO.getOrderGoodsVOS();
        ordersVO.setOrderGoodsVOS(null);
//        String url="http://shop-user:8882/user/detail/"+ordersVO.getUserId();
//        oderDetails.user=(User) RestUtil.getObject(restTemplate,url,User.class);
        oderDetails.user=userService.iGetUserById(ordersVO.getUserId());
        oderDetails.ordersVO=ordersVO;
        return CommonResponse.success(oderDetails);
    }

    /**
     * 更改订单状态
     * @param ordersDTO{orderId:xxx,ordersStatus:xxx}
     * @return
     */
    @PostMapping("/update")
    public CommonResponse updateOrderStatus(@LoginUser Long userId,@RequestBody @Valid OrdersDTO ordersDTO){
        ordersDTO.setUserId(userId);
        OrdersVO ordersVO=orderService.updateOrderStatus(ordersDTO);
        BaseVO<OrdersVO> baseVO=new BaseVO<>();
        List<OrdersVO> ordersVOList=new ArrayList<>();
        ordersVOList.add(ordersVO);
        baseVO.setList(ordersVOList);
        return CommonResponse.success(baseVO);
    }

    /**
     * 提交订单
     * @param userId
     * @param ordersSubDTO {userId:xxx, cartId:xxx, addressId:xxx, couponId:xxx, message:xxx, grouponRulesId:xxx, grouponLinkId:xxx }
     * @return
     */
    @PostMapping("/submit")
    public CommonResponse submitOrder(@LoginUser Long userId,@RequestBody @Valid OrdersSubDTO ordersSubDTO){
        ordersSubDTO.setUserId(userId);
        return orderService.submitOrders(ordersSubDTO);
    }

    @Data
    class OderDetails implements Serializable {
        Set<OrderGoodsVO> orderGoods;
        User user;
        OrdersVO ordersVO;
    }

    //更改团购订单状态
    @PostMapping("/updateGroupOrder")
    public CommonResponse updateGroupOrderStatus(@RequestParam("orderId") Long orderId){
        orderService.updateGroupOrderStatus(orderId);
        return CommonResponse.message();
    }

    /**
     *
     * @param ordersDTO
     * @return
     */
    @PostMapping("/ilist")
    public CommonResponse iselectLimit(Long userId,@RequestBody @Valid OrdersDTO ordersDTO){
        IPage<OrdersVO> ordersVOIPage = orderService.listOrderLimit(ordersDTO);
        BaseVO<OrdersVO> ordersVOBaseVO=new BaseVO<>(ordersVOIPage);
        return CommonResponse.success(ordersVOBaseVO);
    }

    @GetMapping("/idetail")
    public CommonResponse iselectById(Long userId,@RequestParam("id") Long id){
        OderDetails oderDetails=new OderDetails();
        OrdersVO ordersVO=orderService.selectOrderById(id);
        oderDetails.orderGoods=ordersVO.getOrderGoodsVOS();
        ordersVO.setOrderGoodsVOS(null);
//        String url="http://shop-user:8882/user/detail/"+ordersVO.getUserId();
//        oderDetails.user=(User) RestUtil.getObject(restTemplate,url,User.class);
        oderDetails.user=userService.iGetUserById(ordersVO.getUserId());
        oderDetails.ordersVO=ordersVO;
        return CommonResponse.success(oderDetails);
    }

    @PostMapping("/iupdate")
    public CommonResponse iupdateOrderStatus(@RequestBody @Valid OrdersDTO ordersDTO){
        OrdersVO ordersVO=orderService.updateOrderStatus(ordersDTO);
        BaseVO<OrdersVO> baseVO=new BaseVO<>();
        List<OrdersVO> ordersVOList=new ArrayList<>();
        ordersVOList.add(ordersVO);
        baseVO.setList(ordersVOList);
        return CommonResponse.success(baseVO);
    }

}
